package com.mywork.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.WorkMeetingListBean;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.Req.GetWorkMeetingDetailReq;
import com.ciko.code.utils.DateUtil;
import com.ciko.code.utils.NetWorkUtils;
import com.ciko.code.utils.Util;
import com.mywork.R;
import com.zxing.qr_codescan.MipcaActivityCapture;


/**
 * 会议详情
 */
public class WorkMeetingDetailActivity
        extends
        BaseActicity {
    private TextView tvTime, tvAddress, tvTopic, tvContent, tvPerson, tvClient;
    private LinearLayout llParent, llClient, llContent;
    private View viewClient, viewContent;
    private Button btn_conference_sign;//会议签到按钮
    private WorkMeetingListBean bean;

    @Override
    protected void initTopBar() {
        setTitle("会议详情");
        enableNavLeftImg();
        enableNavRightImg(R.drawable.icon_zxing);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_work_meeting_detail);
    }

    @Override
    protected void initView() {
        tvClient = findViewByID(R.id.tv_client);
        tvPerson = findViewByID(R.id.tv_person);
        tvTopic = findViewByID(R.id.tv_topic);
        tvTime = findViewByID(R.id.tv_time);
        tvAddress = findViewByID(R.id.tv_address);
        tvContent = findViewByID(R.id.tv_content);
        llParent = findViewByID(R.id.ll);
        llParent.setVisibility(View.GONE);
        llClient = findViewByID(R.id.ll_client);
        llContent = findViewByID(R.id.ll_content);
        viewClient = findViewByID(R.id.view_client);
        viewContent = findViewByID(R.id.view_content);
        btn_conference_sign = findViewByID(R.id.btn_conference_sign);
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        if (!getIntent().getExtras().containsKey("meetingId")) {
            return;
        }
        int meetingId = getIntent().getIntExtra("meetingId", -1);
        HttpClient.getInstance(this).post(new GetWorkMeetingDetailReq(meetingId), this);
    }

    @Override
    protected void initListener() {
        btn_conference_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //定位签到
                Intent intent = new Intent(WorkMeetingDetailActivity.this,WorkMeetingDetailSignActivity.class);
                intent.putExtra("WorkMeetingListBean",bean);
                startActivityForResult(intent,1);
            }
        });
        setOnNavRightClickListener(new OnNavRightClickListener() {//二维码签到
            @Override
            public void onNavRightClick() {
                Intent intent = new Intent(WorkMeetingDetailActivity.this, MipcaActivityCapture.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        super.onResponseSuccess(response, tag);
        llParent.setVisibility(View.VISIBLE);
        bean = (WorkMeetingListBean) response.getReturnObject();
        if (bean.getCustomer().equals("")) {
            llClient.setVisibility(View.GONE);
            viewClient.setVisibility(View.GONE);
        }
        if (bean.getContent().equals("")) {
            llContent.setVisibility(View.GONE);
            viewContent.setVisibility(View.GONE);
        }
        tvContent.setText(bean.getContent());
        String strTime = DateUtil.longToDate(bean.getMeetingTime(), 8);
//		tvTime.setText(strTime.substring(0, 4) + "年" + strTime.substring(5, 7) + "月" + strTime.substring(8, 10) + "日" + " " + strTime.substring(11, 13) + "点");
        tvTime.setText(strTime.substring(5, 7) + "月" + strTime.substring(8, 10) + "日" + "  " + strTime.substring(11, 13) + "点" + strTime.substring(14, 16) + "分");
        tvAddress.setText(bean.getAddress());
        tvPerson.setText(bean.getAttendees());
        tvTopic.setText(bean.getTitle());
        tvClient.setText(bean.getCustomer());

        if (bean.getSignMode() == 2) {//无定位签到
            btn_conference_sign.setVisibility(View.GONE);
        } else if ("y".equals(bean.getIsSign())) {//已签到
            btn_conference_sign.setEnabled(false);
            btn_conference_sign.setText("已签到");
            btn_conference_sign.setBackgroundResource(R.drawable.qiantui_bg);
        }
//        switch (bean.getType()) {//TODO 通过后台返回的数据进行判断签到按钮的显示
//            case 1://无定位签到
//                btn_conference_sign.setVisibility(View.GONE);
//                break;
//            case 2://已签到
//                btn_conference_sign.setVisibility(View.VISIBLE);
//                btn_conference_sign.setEnabled(false);
//                btn_conference_sign.setText("已签到");
//                break;
//            case 3://已过期
//                btn_conference_sign.setVisibility(View.VISIBLE);
//                btn_conference_sign.setEnabled(false);
//                btn_conference_sign.setBackgroundResource(R.drawable.btn_selected);
//                btn_conference_sign.setText("已过期");
//                break;
//            default://可签到
//                break;
//        }
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        super.onResponseFail(tag, resultCode);
        if (NetWorkUtils.isConnected(this)) {
            Util.showToast(this, "加载失败，请重试");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == WorkMeetingDetailSignActivity.SIGN_CODE) {
            finish();
        }
    }
}
